<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller{

	public $login_expire = 3600;
	public function __construct(){
		parent::__construct();
	}
	public function index(){
		/*if(getBrowser()!="Chrome" && getBrowser()!="Firefox"){  	 //判断getBrowser()只要不等于Chrome Firefox
			$this->browser = getBrowser();                      	//
		}*/
		if( $_POST["act"] == "login"){                          	 //判断 POST 的值是否等于"login"
			if($this->check_verify($_POST['code'])){	    		 //判断验证码加密的 等于 浏览器缓存的值				
				$Aduser = 	D('Aduser');  						     //实例化Aduser数据模型
				$data['adname'] = $_POST['username'];		
				$data['adpw'] = md5($_POST['password']);
				$AduserArr = $Aduser->where($data)->find();   	     //查询单条数据 返值$AduserArr
				if($AduserArr){								   //判断当前是否为$AduserArr
					$id = $AduserArr['id'];                    //初始化 $AduserArr['id'] 
					$data1 = array(                               
								"loginnum"=>$AduserArr['loginnum']+1, //
								"lastloginip"=>get_client_ip(),     //访问地址
								"lastlogintime"=>time(),            //时间戳
					         );			
					$Aduser->where('id='.$id)->save($data1);        //更新数据
					//用户管理员数组
					unset($AduserArr['adpw']);
					
					$session = array(
								  "sversion"=>SESSION_VERSION,//session版本
								  "user"=>$AduserArr,
								  "username"=>$AduserArr["adname"],
								  "effective"=>time()+60*60,//有效期一个小时
								  "chrome"=>array(
								   "uid"=>$AduserArr['uid'],
								  ),
							    );
					session(getOnlyName("admin"),$session);   // 保存变量唯一值
					$this->redirect("Index/index");
					return;
				}else{
					$this->msg = "提示:帐号或密码有误!";    //当前消息显示为 "提示:帐号或密码有误!";  
				}
      		}   
      		else{
				$this->msg = "提示:验证码错误!";   		   //当前消息显示为 "提示:验证码错误!";    
     		}
		}
		$this->display("login");//输出对应的模板为登录页面      
	}

	/*
	*	验证验证码
	*/
	private function check_verify($code, $id = ''){
		$verify = new \Think\Verify();
		return $verify->check($code, $id);
	}
	
	/** 
	 *  
	 * 验证码生成 
	 */  
	public function verify(){  
		$Verify = new \Think\Verify();
		$Verify->fontSize = 20;  
		$Verify->length   = 4;  
		$Verify->useNoise = true;  
		$Verify->codeSet = '0123456789';  
		$Verify->imageW = 130;  
		$Verify->imageH = 50;  
		//$Verify->expire = 600;  
		$Verify->entry();  
	} 
	public function _empty(){   
	  header("HTTP/1.0 404 Not Found"); 
      $this->display('default:error/404');  
	}
}
?>